package cn.ulc.socket.admin.common;

import cn.ulc.common.constant.enums.DeletedStatus;
import cn.ulc.common.constant.enums.ResultCode;
import cn.ulc.common.exception.PlatformException;
import cn.ulc.common.util.UserUtils;
import cn.ulc.store.entity.Company;
import cn.ulc.store.entity.base.BaseModel;
import cn.ulc.store.entity.staff.Staff;

import java.util.Date;

public class BaseService extends cn.ulc.common.base.BaseService {

    /**
     * 判断对象是否为空
     * @param baseModel
     * @return
     */
    public boolean isNullObject(BaseModel baseModel) {
        return (baseModel == null || baseModel.getId() == null || baseModel.getId() <= 0);
    }
    /**
     * 设置基础信息字段
     * @param baseModel
     * @return
     */
    public BaseModel setBaseInfo (BaseModel baseModel) {
        Object object = UserUtils.getUser();
        Staff staff = (Staff) object;
        if(staff == null) return null;
        if(baseModel.getId() == null || baseModel.getId() == 0) {
            baseModel.setCreatorId(staff.getId());
            baseModel.setCreateAt(new Date());
            baseModel.setDeleted(DeletedStatus.UN_DELETED);
            baseModel.setCompanyId(staff.getCompanyId());
        }else {
            baseModel.setModifierId(staff.getId());
            baseModel.setModifyAt(new Date());
        }
        return baseModel;
    }


    public Staff getCurrentUser() {
        Object object = UserUtils.getUser();
        Staff user = (Staff)object;
        if(user == null) throw new PlatformException(ResultCode.NO_LOGIN.getCode(), "登录信息已过期");
        return user;
    }

    public Company getCurrentCompany() {
        Object object = UserUtils.getCompany();
        Company company = (Company)object;
        if(company == null) throw new PlatformException(ResultCode.NO_LOGIN.getCode(), "登录信息已过期");
        return company;
    }

}
